package com.ogawa.projectcommon.widget

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.ogawa.projectcommon.R
import com.ogawa.projectcommon.bean.MovementBean
import kotlinx.android.synthetic.main.dialog_jx_edit.*
import kotlinx.android.synthetic.main.dialog_jx_edit.tr_view
import kotlinx.android.synthetic.main.dialog_jx_edit.tv_no
import kotlinx.android.synthetic.main.dialog_jx_edit.tv_yes

class JxEditDialog(context: Context) : BottomSheetDialog(context, R.style.BottomSheetDialogStyle),
    View.OnClickListener {
    private var sideType = -1
    private var handType = -1
    private val handTypes = arrayOf(
        R.string.knead,
        R.string.knock,
        R.string.clap,
        R.string.swedish,
        R.string.zhiya,
        R.string.tuina
    )

    init {
        setContentView(R.layout.dialog_jx_edit)
        delegate.findViewById<View>(com.ogawa.base.R.id.design_bottom_sheet)?.setBackgroundColor(
            Color.TRANSPARENT
        )
        tv_neck.setOnClickListener(this)
        tv_shoulder.setOnClickListener(this)
        tv_up_back.setOnClickListener(this)
        tv_down_back.setOnClickListener(this)
        tv_back.setOnClickListener(this)
        tv_hip.setOnClickListener(this)
        tv_tuina.setOnClickListener(this)
        tv_knock.setOnClickListener(this)
        tv_clap.setOnClickListener(this)
        tv_swedish.setOnClickListener(this)
        tv_zhiya.setOnClickListener(this)
        tv_knead.setOnClickListener(this)
        tv_no.setOnClickListener(this)
        lev_view_4d.setLev(1)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tv_neck -> setSideType(0)
            R.id.tv_shoulder -> setSideType(1)
            R.id.tv_up_back -> setSideType(2)
            R.id.tv_down_back -> setSideType(3)
            R.id.tv_back -> setSideType(4)
            R.id.tv_hip -> setSideType(5)
            R.id.tv_tuina -> setHandType(5)
            R.id.tv_knock -> setHandType(1)
            R.id.tv_clap -> setHandType(2)
            R.id.tv_swedish -> setHandType(3)
            R.id.tv_zhiya -> setHandType(4)
            R.id.tv_knead -> setHandType(0)
            R.id.tv_no -> dismiss()
        }
    }

    fun show(movementBean: MovementBean? = null) {
        if (movementBean != null) {
            setHandType(movementBean.handType)
            tr_view.setTime(movementBean.startTime, movementBean.endTime)
            lev_jx_strength.setLev(movementBean.strongRank)
            lev_speed.setLev(movementBean.speedRank)
            lev_view_4d.setLev(movementBean.gearDRank)
            setSideType(movementBean.sideType)
            wlv_width.setLev(movementBean.widthRank)
        }
        show()
    }

    private fun setHandType(handType: Int) {
        if (handType == 0 || handType == 3) {//手法为揉捏或瑞典时，宽度不可调节
            wlv_width.setLev(0)
        } else {
            wlv_width.setLev(1)
        }
        if (handType == 0) {//手法为揉捏时机芯可调节，其他手法机芯强度不可调
            lev_jx_strength.setLev(1)
        } else {
            lev_jx_strength.setLev(0)
        }
        if (handType == 0 || handType == 5) {//手法为揉捏或推拿时，机芯速度不可调节
            lev_speed.setLev(0)
        } else {
            lev_speed.setLev(1)
        }
        tv_knead.isSelected = handType == 0
        tv_knock.isSelected = handType == 1
        tv_clap.isSelected = handType == 2
        tv_swedish.isSelected = handType == 3
        tv_zhiya.isSelected = handType == 4
        tv_tuina.isSelected = handType == 5
        this.handType = handType
    }

    fun isTimeLegal(): Boolean {
        return tr_view.isTimeLegal()
    }

    fun setConfirmClickListener(listener: View.OnClickListener) {
        tv_yes.setOnClickListener(listener)
    }

    fun getTimeRangeView(): TimeRangeView {
        return tr_view
    }

    private fun setSideType(area: Int) {
        sideType = area
        tv_neck.isSelected = area == 0
        tv_shoulder.isSelected = area == 1
        tv_up_back.isSelected = area == 2
        tv_down_back.isSelected = area == 3
        tv_back.isSelected = area == 4
        tv_hip.isSelected = area == 5
    }

    fun getWidth(): Int {
        return wlv_width.getLev()
    }

    fun getSideType(): Int {
        return sideType
    }

    fun getStrongRank(): Int {
        return lev_jx_strength.getLev()
    }

    fun getSpeedRank(): Int {
        return lev_speed.getLev()
    }

    fun getHandType(): Int {
        return handType
    }

    fun get5DRank(): Int {
        return lev_view_4d.getLev()
    }

    fun getStartTime(): Int {
        return tr_view.getStartTime()
    }

    fun getEndTime(): Int {
        return tr_view.getEndTime()
    }
}